// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CoordinateTransform3D.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Numerics
{
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Runtime.Serialization;

    /// <summary>
    /// Class used to cache the values needed for a rigid 3D transformation
    /// Takes a pose or a point and warps it using a rigid transformation
    /// out = (R*in + t) .* scale
    /// </summary>
    [DataContract]
    public class CoordinateTransform3D
    {
        /// <summary>
        /// Scale factor
        /// </summary>
        private Vector3 scale;

        /// <summary>
        /// The inverse of the orientation
        /// </summary>
        private Quaternion inverse;

        /// <summary>
        /// Initializes a new instance of the CoordinateTransform3D class.
        /// The resulting instance can be used to transform points from one coordinate frame to another (rigid transformation).
        /// </summary>
        public CoordinateTransform3D()
            : this(new Pose(new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 1)), new Vector3(1, 1, 1))
        {
        }

        /// <summary>
        /// Initializes a new instance of the CoordinateTransform3D class.
        /// The resulting instance can be used to transform points from one coordinate frame to another (rigid transformation).
        /// </summary>
        /// <param name="baseCoordinateFrame">The coordinate frame to transform to (rotate + translate)</param>
        public CoordinateTransform3D(Pose baseCoordinateFrame)
            : this(baseCoordinateFrame, new Vector3(1, 1, 1))
        {
        }

        /// <summary>
        /// Initializes a new instance of the CoordinateTransform3D class.
        /// The resulting instance can be used to transform points from one coordinate frame to another (rigid transformation).
        /// </summary>
        /// <param name="baseCoordinateFrame">The coordinate frame to transform to (rotate + translate)</param>
        /// <param name="scale">Scaling factor</param>
        public CoordinateTransform3D(Pose baseCoordinateFrame, Vector3 scale)
        {
            this.Pose = baseCoordinateFrame;
            this.scale = scale;
            this.inverse = Quaternion.Inverse(this.Pose.Orientation);
        }

        /// <summary>
        /// Gets the coordinate frame of the original pose
        /// </summary>
        [DataMember]
        public Pose Pose { get; private set; }

        /// <summary>
        /// Reverses the transformation
        /// </summary>
        /// <returns>The reverse transformation</returns>
        public CoordinateTransform3D Reverse()
        {
            Vector3 translation = Quaternion.Rotate(this.inverse, -this.Pose.Position);

            CoordinateTransform3D newTransform = new CoordinateTransform3D(
                new Pose(translation, this.inverse),
                new Vector3(1 / this.scale.X, 1 / this.scale.Y, 1 / this.scale.Z));
            return newTransform;
        }

        /// <summary>
        /// Transforms a pose
        /// </summary>
        /// <param name="pose">Pose to transform</param>
        /// <returns>The transformed pose</returns>
        public Pose Transform(Pose pose)
        {
            Quaternion newOrientation = this.Pose.Orientation * pose.Orientation;
            Vector3 newTranslation = Quaternion.Rotate(this.Pose.Orientation, pose.Position) + this.Pose.Position;

            return new Pose(new Vector3(this.scale.X * newTranslation.X, this.scale.Y * newTranslation.Y, this.scale.Z * newTranslation.Z), newOrientation);
        }

        /// <summary>
        /// Transforms a point
        /// </summary>
        /// <param name="point">point to transform</param>
        /// <returns>The transformed point</returns>
        public Vector3 Transform(Vector3 point)
        {
            Vector3 newTranslation = Quaternion.Rotate(this.Pose.Orientation, point) + this.Pose.Position;
            return new Vector3(this.scale.X * newTranslation.X, this.scale.Y * newTranslation.Y, this.scale.Z * newTranslation.Z);
        }
    }
}
